from django.shortcuts import render,HttpResponse,redirect #重定向
from web.models import Admin,Students,Depart

# Create your views here.
#管理员登陆
def login(request):
    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')
        obj = Admin.objects.filter(ad_name=username,ad_password=password)
        if obj:
            return render(request,'home.html')
        return render(request, 'login.html', {"error": "用户名或密码错误！"})
    return render(request, 'login.html')

#主界面
def home(request):
    return render(request,'home.html')


#管理员注册
def regiest_admin(request):
    if request.method == 'GET':
        return render(request, "admin/regiest_admin.html")
    id = request.POST.get('id')
    username = request.POST.get('username')
    password = request.POST.get('password')
    obj = Admin.objects.filter(id=id)
    if obj:
        return render(request,'admin/regiest_admin.html',{"error": "用户已存在！"})
    Admin.objects.create(id=id,ad_name=username,ad_password=password)
    return redirect('/home_admin/')
#管理员删除
def delete_admin(request):
    id = request.GET.get('id')
    Admin.objects.filter(id=id).first().delete()
    return redirect("/home_admin/")
#管理员修改
def edit_admin(request):
    id = request.GET.get('id')
    obj = Admin.objects.filter(id=id).first()
    if request.method == 'GET':
        return render(request,'admin/edit_admin.html',{"obj":obj})
    username = request.POST.get('username')
    password = request.POST.get('password')
    Admin.objects.filter(id=id).update(ad_name=username,ad_password=password)
    return redirect("/home_admin/")
#管理员目录
def home_admin(request):
    queryset = Admin.objects.all()
    return render(request, 'admin/home_admin.html', {'queryset': queryset})


#学生注册
def regiest_stu(request):
    if request.method == 'GET':
        return render(request, "stu/regiest_stu.html")
    s_id = request.POST.get('s_id')
    name = request.POST.get('name')
    sex = request.POST.get('sex')
    age = request.POST.get('age')
    college = request.POST.get('college')
    speciality = request.POST.get('speciality')
    phone = request.POST.get('phone')
    adress = request.POST.get('adress')
    depart_id = request.POST.get('depart_id')
    obj = Students.objects.filter(s_id=s_id)
    if obj:
        return render(request,'stu/regiest_stu.html',{"error": "该学号已存在！"})
    Students.objects.create(
        s_id=s_id,s_name=name,s_sex=sex,s_age=age,s_college=college,
        s_speciality=speciality,s_phone=phone,s_adress=adress,depart_id=depart_id)
    return redirect('/home_stu/')
#学生删除
def delete_stu(request):
    s_id = request.GET.get('s_id')
    Students.objects.filter(s_id=s_id).first().delete()
    return redirect("/home_stu/")
#学生修改
def edit_stu(request):
    s_id = request.GET.get('s_id')
    obj = Students.objects.filter(s_id=s_id).first()
    if request.method == 'GET':
        return render(request, 'stu/edit_stu.html', {"obj": obj})
    name = request.POST.get('name')
    sex = request.POST.get('sex')
    age = request.POST.get('age')
    college = request.POST.get('college')
    speciality = request.POST.get('speciality')
    phone = request.POST.get('phone')
    adress = request.POST.get('adress')
    depart_id = request.POST.get('depart_id')
    Students.objects.filter(s_id=s_id).update(
        s_name=name, s_sex=sex,s_age=age,s_college=college,s_speciality=speciality
        ,s_phone=phone,s_adress=adress,depart_id=depart_id)
    return redirect("/home_stu/")
#学生目录
def home_stu(request):
    queryset = Students.objects.all()
    return render(request, "stu/home_stu.html",{'queryset': queryset})




#宿舍注册
def regiest_depart(request):
    return render(request, "depart/regiest_depart.html")
#宿舍删除
def delete_depart(request):
    return render(request, "depart/delete_depart.html")
#宿舍修改
def edit_depart(request):
    return render(request, "depart/edit_depart.html")
#宿舍目录
def home_depart(request):
    id = Depart.objects.filter(id)
    name = Students.objects.filter(depart_id=id)
    print(name)
    queryset = Depart.objects.all()
    return render(request, "depart/home_depart.html", {'queryset': queryset})



